package com.ias.agdis_r.fragment.task;

import android.os.Bundle;
//import android.support.annotation.Nullable;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseFragment;
import com.ias.agdis_r.activity.LuggageBarCodeActivity;
import com.ias.agdis_r.adapter.LuggageBarCodeAdapter;
import com.ias.agdis_r.bean.task.LuggageBarCode;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.util.S;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xr.g on 2018/3/13.
 */

public class LuggageBarCodeUploading extends BaseFragment {
    private List<LuggageBarCode> list = new ArrayList<>();
    private LuggageBarCodeAdapter adapter;
    private String tnb;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.luggagebarcode_uploading, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        resetDate();
    }

    @Override
    protected void operateResponseSucess(String serviceId, BaseResponse response) {
    }

    @Override
    protected void updateUI(String tag) {
        if (LuggageBarCodeActivity.refreshTabUploading.equals(tag)) {
            resetDate();
        } else if (MessageModel.ULBC.equals(tag) || MessageModel.DLBC.equals(tag)
                || MessageModel.QLBC.equals(tag)) {
            resetDate();
        }
    }

    private void initView() {
        ListView lvBarCode = (ListView) getView().findViewById(R.id.lvluggageBarCode);
        adapter = new LuggageBarCodeAdapter(getActivity(), list, false);
        lvBarCode.setAdapter(adapter);
    }

    private void resetDate() {
        if (S.isNotEmpty(tnb)) {
            list.clear();
            BoxDBHelper dbHelper = new BoxDBHelper(getActivity());
            List<LuggageBarCode> luggageBarCodeList = dbHelper.getUploadingLuggageBarCode(tnb);
            list.addAll(luggageBarCodeList);
            dbHelper.closeDB();
            adapter.notifyDataSetChanged();
        }
    }

    public void setTnb(String tnb) {
        this.tnb = tnb;
        resetDate();
    }
}
